package eth

import (
	"bytes"
	"github.com/ethereum/go-ethereum/accounts/abi"
	"github.com/ethereum/go-ethereum/ethclient"
	"io"
	"log"
	"ruoyi-go/app/service"
	"ruoyi-go/common/upload"
)

func getClient(rpc string, abi_bytes []byte) (client *ethclient.Client, abiJson *abi.ABI, err error) {
	contractApi, err := abi.JSON(bytes.NewReader(abi_bytes))
	if err != nil {
		log.Fatal(err)
	}
	client, err = ethclient.Dial(rpc)
	defer client.Close()

	return client, &contractApi, err
}

func GetClient(id int32, rpc string) (client *ethclient.Client, abiJson *abi.ABI, err error) {
	data := (&service.FileService{}).GetFileData(id)
	file := upload.GetSmallFile(data.FilePath)
	all, err := io.ReadAll(file)
	if err != nil {
		log.Fatal(err)
	}
	return getClient(rpc, all)
}

//func ()  {
//
//}
